<?php

/**
 * Created by novebeta.
 * Date: 10/21/12
 * Time: 6:01 PM
 */
/**
 * This is the shortcut to DIRECTORY_SEPARATOR
 */
defined('DS') or define('DS', DIRECTORY_SEPARATOR);

defined('ATAS') or define('ATAS', 0);
defined('PUSAT') or define('PUSAT', 1);
defined('TIMUR') or define('TIMUR', 11);
defined('TIMURSELATAN') or define('TIMURSELATAN', 111);
defined('TIMURTENGAH') or define('TIMURTENGAH', 112);
defined('TIMURUTARA') or define('TIMURUTARA', 113);
defined('TENGAH') or define('TENGAH', 12);
defined('TENGAHSELATAN') or define('TENGAHSELATAN', 121);
defined('TENGAHTENGAH') or define('TENGAHTENGAH', 122);
defined('TENGAHTIMUR') or define('TENGAHTIMUR', 123);
defined('BARAT') or define('BARAT', 13);
defined('BARATBARAT') or define('BARATBARAT', 131);


//defined('KOLASPRO') or define('KOLASPRO', 1);
//defined('LASPRO') or define('LASPRO', 11);
//defined('KOLASTEN') or define('KOLASTEN', 2);
//defined('LASTEN') or define('LASTEN', 22);
//defined('KOLASMAT') or define('KOLASMAT', 3);
//defined('LASMAT') or define('LASMAT', 33);
//defined('KOLASRAH') or define('KOLASRAH', 4);
//defined('LASRAH') or define('LASRAH', 44);
//defined('KOLASSA') or define('KOLASSA', 5);
//defined('LASSA') or define('LASSA', 55);

global $struktur_org, $region_wilayah, $penguruskabupaten, $penguruskecamatan, $penguruskelurahan, $pengurusdesa;
$penguruskabupaten = array(
    1 => "KOLASTEN",
    2 => "LASTEN"
);

$penguruskecamatan = array(
    1 => "KOLASMAT",
    2 => "LASMAT",
);

$penguruskelurahan = array(
    1 => "KOLASRAH",
    2 => "LASRAH",
);

$pengurusdesa = array(
    1 => "KOLASSA",
    2 => "LASSA",
);


$struktur_org = array(
    /* ATAS => "Pendiri Sultan Fatah 81",
      PUSAT => "Pimpinan Sultan Fatah 81",
      TIMUR => "Koordinator Region Timur",
      TIMURSELATAN => "Asisten Wilayah Selatan - Region Timur",
      TIMURTENGAH => "Asisten Wilayah Tengah - Region Timur",
      TIMURUTARA => "Asisten Wilayah Utara - Region Timur",
      TENGAH => "Koordinator Region Tengah",
      TENGAHSELATAN => "Asisten Wilayah Selatan - Region Tengah",
      TENGAHTENGAH => "Asisten Wilayah Tengah - Region Tengah",
      TENGAHTIMUR => "Asisten Wilayah Timur - Region Tengah",
      BARAT => "Koordinator Region Barat",
      BARATBARAT => "Asisten Wilayah Barat - Region Barat", */
    ATAS => "Pendiri Sultan Fatah 81",
    PUSAT => "Pimpinan Sultan Fatah 81",
    TIMUR => "Laspro Region Timur",
    TIMURSELATAN => "Aswil Selatan - Region Timur",
    TIMURTENGAH => "Aswil Tengah - Region Timur",
    TIMURUTARA => "Aswil Utara - Region Timur",
    TENGAH => "Laspro Region Tengah",
    TENGAHSELATAN => "Aswil Wilayah Selatan - Region Tengah",
    TENGAHTENGAH => "Aswil Wilayah Tengah - Region Tengah",
    TENGAHTIMUR => "Aswil Wilayah Timur - Region Tengah",
    BARAT => "Laspro Region Barat",
    BARATBARAT => "Aswil Wilayah Barat - Region Barat",
);
$region_wilayah = array(
    TIMURSELATAN => "Wilayah Selatan - Region Timur",
    TIMURTENGAH => "Wilayah Tengah - Region Timur",
    TIMURUTARA => "Wilayah Utara - Region Timur",
    TENGAHSELATAN => "Wilayah Selatan - Region Tengah",
    TENGAHTENGAH => "Wilayah Tengah - Region Tengah",
    TENGAHTIMUR => "Wilayah Timur - Region Tengah",
    BARATBARAT => "Wilayah Barat - Region Barat",
);

defined('SUPERADMIN') or define('SUPERADMIN', 1000);
defined('ASWIL') or define('ASWIL', 50);
defined('KABUPATEN') or define('KABUPATEN', 10);
global $role_login;
$role_login = array(
    SUPERADMIN => "Super Administrator",
    ASWIL => "Asisten Wilayah",
    KABUPATEN => "Koordinator Kabupaten",
);

global $array_hari, $array_pasaran;
$array_hari = array("Senin" => "Senin", "Selasa" => "Selasa", "Rabu" => "Rabu",
    "Kamis" => "Kamis", "Jumat" => "Jumat", "Sabtu" => "Sabtu", "Minggu" => "Minggu");
$array_pasaran = array("Legi" => "Legi", "Pon" => "Pon", "Kliwon" => "Kliwon",
    "Pahing" => "Pahing", "Wage" => "Wage");
global $array_gender, $bentuk_kegiatan;
$array_gender = array("Pria", "Wanita");
$bentuk_kegiatan = array("Khusuyiah" => "Khusuyiah", "Rutinan" => "Rutinan");

/**
 * This is the shortcut to Yii::app()
 */
function app() {
    return Yii::app();
}

function root() {
    return Yii::getPathOfAlias('webroot');
}

/**
 * This is the shortcut to Yii::app()->clientScript
 */
function cs() {
    // You could also call the client script instance via Yii::app()->clientScript
    // But this is faster
    return Yii::app()->getClientScript();
}

/**
 * This is the shortcut to Yii::app()->user.
 */
function user() {
    return Yii::app()->getUser();
}

function username() {
    $user = oUser();
    return $user->user_name;
}

function oUser() {
    $id = user()->getId();
    return Users::model()->findByPk($id);
}

/**
 * This is the shortcut to Yii::app()->createUrl()
 */
function url($route, $params = array(), $ampersand = '&') {
    return Yii::app()->createUrl($route, $params, $ampersand);
}

/**
 * This is the shortcut to CHtml::encode
 */
function h($text) {
    return htmlspecialchars($text, ENT_QUOTES, Yii::app()->charset);
}

/**
 * This is the shortcut to Yii::app()->language (current language)
 */
function lang() {
    return Yii::app()->language;
}

/**
 * This is the shortcut to CHtml::link()
 */
function l($text, $url = '#', $htmlOptions = array()) {
    return CHtml::link($text, $url, $htmlOptions);
}

function img($src, $alt = "", $htmlOptions = array()) {
    return CHtml::image($src, $alt, $htmlOptions);
}

function e($text) {
    return CHtml::encode($text);
}

/**
 * This is the shortcut to Yii::t() with default category = 'stay'
 */
function t($message, $category = 'stay', $params = array(), $source = null,
        $language = null) {
    return Yii::t($category, $message, $params, $source, $language);
}

/**
 * This is the shortcut to Yii::app()->request->baseUrl
 * If the parameter is given, it will be returned and prefixed with the app baseUrl.
 */
function bu($url = null) {
    static $baseUrl;
    if ($baseUrl === null) $baseUrl = Yii::app()->getRequest()->getBaseUrl();
    return $url === null ? $baseUrl : $baseUrl . '/' . ltrim($url, '/');
}

/**
 * Returns the named application parameter.
 * This is the shortcut to Yii::app()->params[$name].
 */
function param($name) {
    return Yii::app()->params[$name];
}

function Encrypt($string) {
    return base64_encode(Yii::app()->getSecurityManager()->encrypt($string));
}

function Decrypt($string) {
    return Yii::app()->getSecurityManager()->decrypt(base64_decode($string));
}

function sql2date($date, $string_pattern = 'dd/MM/yyyy') {
    $timestamp = CDateTimeParser::parse($date, 'yyyy-MM-dd');
    return Yii::app()->dateFormatter->format($string_pattern, $timestamp);
}

function date2sql($date, $string_pattern = 'dd/MM/yyyy') {
    $timestamp = CDateTimeParser::parse($date, $string_pattern);
    return Yii::app()->dateFormatter->format('yyyy-MM-dd', $timestamp);
}

function sql2datetime($date, $string_pattern = 'dd/MM/yyyy hh:mm:ss') {
    $timestamp = CDateTimeParser::parse($date, 'yyyy-MM-dd hh:mm:ss');
    return Yii::app()->dateFormatter->format($string_pattern, $timestamp);
}

function datetime2sql($date, $string_pattern = 'dd/MM/yyyy hh:mm:ss') {
    $timestamp = CDateTimeParser::parse($date, $string_pattern);
    return Yii::app()->dateFormatter->format('yyyy-MM-dd hh:mm:ss', $timestamp);
}

function sql2long_date($date) {
    $timestamp = CDateTimeParser::parse($date, 'yyyy-MM-dd');
    $formater = new CDateFormatter('id_ID');
    return $formater->formatDateTime($timestamp, 'long', false);
}

function get_date_tomorrow() {
    return Yii::app()->dateFormatter->format('yyyy-MM-dd',
                    time() + (1 * 24 * 60 * 60));
}

function get_time_now() {
    return Yii::app()->dateFormatter->format('HH:mm:ss', time());
}

function Now($format = 'yyyy-MM-dd HH:mm:ss') {
    return Yii::app()->dateFormatter->format($format, time());
}

function period2date_now() {
    $timestamp = new DateTime();
    $start = $timestamp->format('Y-m-d');
    $end = $timestamp->format('Y-m-t');
    return array('start' => $start, 'end' => $end);
}

function get_date_today($format = 'yyyy-MM-dd') {
    return Yii::app()->dateFormatter->format($format, time());
}

function random_string($length, $include_number = false) {
    $original_string = $include_number ? array_merge(range(0, 9),
                    range('a', 'z'), range('A', 'Z')) :
            array_merge(range('a', 'z'), range('A', 'Z'));
    $original_string = implode("", $original_string);
    return substr(str_shuffle($original_string), 0, $length);
}
